function ChkUtil() { }
// 校验是否为空(先删除二边空格再验证)
ChkUtil.isNull = function (str) {
  str = str.replace(/(^\s*)|(\s*$)/g, "");// 删除二边空格
  if (null == str || "" == str) {
    return true;
  } else {
    return false;
  }
};
//判断是否只有英文
ChkUtil.isEnglish = function (str) {
  var patrn = /^[a-z]+$/i;
  return patrn.test(str);
};

/*获取字符串长度，中文算两个字符，英文算一个字符*/
ChkUtil.getLength = function (str) {
  str = str.replace(/(^\s*)|(\s*$)/g, "");//删除二边空格
  var totallength = 0;
  for (var i = 0; i < str.length; i++) {
    var intCode = str.charCodeAt(i);
    if (intCode >= 0 && intCode <= 128) {
      totallength = totallength + 1; //非中文单个字符长度加 1
    } else {
      totallength = totallength + 2; //中文字符长度则加 2
    }
  }
  return totallength;
};
//校验是否仅中文
ChkUtil.isChinese = function (str) {
  var patrn = /[\u4E00-\u9FA5\uF900-\uFA2D]+$/;
  return patrn.test(str);
};
//判断是否只含有中文、字母最多26个
ChkUtil.isChanEng1_26 = function (str) {
  str = str.replace(/(^\s*)|(\s*$)/g, "");// 删除二边空格
  var patrn = /^([\u4e00-\u9fa5]{1,13}|[a-zA-Z]{2,26})$/;
  return patrn.test(str);
};

//判断是否只含有中文、字母、数字
ChkUtil.isChanEng = function (str) {
  str = str.replace(/(^\s*)|(\s*$)/g, "");// 删除二边空格
  var patrn = /^[a-z\u4E00-\u9FA5]+$/i;
  return patrn.test(str);
};
//判断是否只含有中文、字母、数字、下划线、(增加)中划线（判断用户名）
ChkUtil.isNickName = function (str) {
  //str = str.replace(/(^\s*)|(\s*$)/g, "");// 删除二边空格
  var patrn = /^[(\w)\d\u4E00-\u9FA5-]+$/i;
  return patrn.test(str);
};
//校验手机号码
ChkUtil.isMobile = function (str) {
  var patrn = /^0?1[3,4,5,6,7,8,9][0-9]{9}$/;
  return patrn.test(str);
};
// 校验电邮地址
ChkUtil.isEmail = function (str) {
  var patrn = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  return patrn.test(str);
};

//是否是身份证(不考虑15位)
ChkUtil.isIdCardNew = function (obj) {
  var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" };
  var iSum = 0;
  var strIDno = obj;
  var idCardLength = strIDno.length;
  var GetVerifyBit = function (id) {
    var result = '';
    var nNum = eval(id.charAt(0) * 7 + id.charAt(1) * 9 + id.charAt(2) * 10 + id.charAt(3) * 5 + id.charAt(4) * 8 + id.charAt(5) * 4 + id.charAt(6) * 2 + id.charAt(7) * 1 + id.charAt(8) * 6 + id.charAt(9) * 3 + id.charAt(10) * 7 + id.charAt(11) * 9 + id.charAt(12) * 10 + id.charAt(13) * 5 + id.charAt(14) * 8 + id.charAt(15) * 4 + id.charAt(16) * 2);
    nNum = nNum % 11;
    switch (nNum) {
      case 0:
        result = "1";
        break;
      case 1:
        result = "0";
        break;
      case 2:
        result = "X";
        break;
      case 3:
        result = "9";
        break;
      case 4:
        result = "8";
        break;
      case 5:
        result = "7";
        break;
      case 6:
        result = "6";
        break;
      case 7:
        result = "5";
        break;
      case 8:
        result = "4";
        break;
      case 9:
        result = "3";
        break;
      case 10:
        result = "2";
        break;
    }
    return result;
  };
  if (!/^\d{17}(\d|x)$/i.test(strIDno) && !/^\d{15}$/i.test(strIDno)) {
    return false; // 非法身份证号
  }

  if (aCity[parseInt(strIDno.substr(0, 2))] == null) {
    return false;// 非法地区
  }

  // 15位身份证转换为18位
  if (idCardLength == 15) {
    //		 sBirthday = "19" + strIDno.substr(6,2) + "-" + Number(strIDno.substr(8,2)) + "-" + Number(strIDno.substr(10,2));
    //		 var d = new Date(sBirthday.replace(/-/g,"/"));
    //		 var dd = d.getFullYear().toString() + "-" + (d.getMonth()+1) + "-" + d.getDate();
    //		 if(sBirthday != dd)
    //		 {
    //           return false; // 非法生日
    //		 }
    //        strIDno=strIDno.substring(0,6)+"19"+strIDno.substring(6,15);
    //        strIDno=strIDno+GetVerifyBit(strIDno);
    return false;
  }

  // 判断是否大于2078年，小于1900年
  var year = strIDno.substring(6, 10);
  if (year < 1900 || year > 2078) {
    return false;// 非法生日
  }

  // 18位身份证处理

  // 在后面的运算中x相当于数字10,所以转换成a
  var strIDno = strIDno.replace(/x$/i, "a");

  var sBirthday = strIDno.substr(6, 4) + "-" + Number(strIDno.substr(10, 2)) + "-" + Number(strIDno.substr(12, 2));
  var d = new Date(sBirthday.replace(/-/g, "/"));
  if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) {
    return false; // 非法生日
  }
  // 身份证编码规范验证
  for (var i = 17; i >= 0; i--) {
    iSum += (Math.pow(2, i) % 11) * parseInt(strIDno.charAt(17 - i), 11);
  }
  if (iSum % 11 != 1) {
    return false;// 非法身份证号
  }

  // 判断是否屏蔽身份证
  var words = new Array();
  words = new Array("11111119111111111", "12121219121212121");

  for (var k = 0; k < words.length; k++) {
    if (strIDno.indexOf(words[k]) != -1) {
      return false;
    }
  }

  return true;
};

//是否是身份证
ChkUtil.isIdCard = function (obj) {
  var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" };
  var iSum = 0;
  var strIDno = obj;
  var idCardLength = strIDno.length;
  var GetVerifyBit = function (id) {
    var result = '';
    var nNum = eval(id.charAt(0) * 7 + id.charAt(1) * 9 + id.charAt(2) * 10 + id.charAt(3) * 5 + id.charAt(4) * 8 + id.charAt(5) * 4 + id.charAt(6) * 2 + id.charAt(7) * 1 + id.charAt(8) * 6 + id.charAt(9) * 3 + id.charAt(10) * 7 + id.charAt(11) * 9 + id.charAt(12) * 10 + id.charAt(13) * 5 + id.charAt(14) * 8 + id.charAt(15) * 4 + id.charAt(16) * 2);
    nNum = nNum % 11;
    switch (nNum) {
      case 0:
        result = "1";
        break;
      case 1:
        result = "0";
        break;
      case 2:
        result = "X";
        break;
      case 3:
        result = "9";
        break;
      case 4:
        result = "8";
        break;
      case 5:
        result = "7";
        break;
      case 6:
        result = "6";
        break;
      case 7:
        result = "5";
        break;
      case 8:
        result = "4";
        break;
      case 9:
        result = "3";
        break;
      case 10:
        result = "2";
        break;
    }
    return result;
  };
  if (!/^\d{17}(\d|x)$/i.test(strIDno) && !/^\d{15}$/i.test(strIDno)) {
    return false; // 非法身份证号
  }

  if (aCity[parseInt(strIDno.substr(0, 2))] == null) {
    return false;// 非法地区
  }

  // 15位身份证转换为18位
  if (idCardLength == 15) {
    var sBirthday = "19" + strIDno.substr(6, 2) + "-" + Number(strIDno.substr(8, 2)) + "-" + Number(strIDno.substr(10, 2));
    var d = new Date(sBirthday.replace(/-/g, "/"));
    var dd = d.getFullYear().toString() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
    if (sBirthday != dd) {
      return false; // 非法生日
    }
    strIDno = strIDno.substring(0, 6) + "19" + strIDno.substring(6, 15);
    strIDno = strIDno + GetVerifyBit(strIDno);
  }

  // 判断是否大于2078年，小于1900年
  var year = strIDno.substring(6, 10);
  if (year < 1900 || year > 2078) {
    return false;// 非法生日
  }

  // 18位身份证处理

  // 在后面的运算中x相当于数字10,所以转换成a
  strIDno = strIDno.replace(/x$/i, "a");

  sBirthday = strIDno.substr(6, 4) + "-" + Number(strIDno.substr(10, 2)) + "-" + Number(strIDno.substr(12, 2));
  var d = new Date(sBirthday.replace(/-/g, "/"));
  if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) {
    return false; // 非法生日
  }
  // 身份证编码规范验证
  for (var i = 17; i >= 0; i--) {
    iSum += (Math.pow(2, i) % 11) * parseInt(strIDno.charAt(17 - i), 11);
  }
  if (iSum % 11 != 1) {
    return false;// 非法身份证号
  }

  // 判断是否屏蔽身份证
  var words = new Array();
  words = new Array("11111119111111111", "12121219121212121");

  for (var k = 0; k < words.length; k++) {
    if (strIDno.indexOf(words[k]) != -1) {
      return false;
    }
  }

  return true;
};

ChkUtil.isCaptcha = function (str) {
  var patrn = /^[\da-zA-Z]{4}$/;
  return patrn.test(str);
};
//容错提示 处理(20141111xuezx暂时还用的老板的，过两天我写弹窗出来)
ChkUtil.errorMessage = function (msg, node1, node2, time) {
  var $light = false;
  var $errorMsg = false;
  time = time && time > 0 ? time : 3;

  if (typeof node1 == 'object' && typeof nide2 == 'object') {
    $light = $(node1);
    $errorMsg = $(node2);
  } else {
    if (document.getElementById('errTipDiv') == null) {
      var tipDiv = document.createElement('div');
      tipDiv.id = 'errTipDiv';
      tipDiv.className = 'errTiShi';
      tipDiv.innerHTML = '<p id="errorMsg"></p>';
      document.body.appendChild(tipDiv);
    }
    $light = $('#errTipDiv');
    $errorMsg = $('#errorMsg');
  }
  try {
    window.scrollTo(0, 0);
    $errorMsg.text(msg);
    $light.slideDown();
    setTimeout(function () { $light.slideUp(); }, time * 1000);
    //alert(msg);
  } catch (err) {
    alert(err.message);
  }
};

/**
 *随便写的弹窗 
 */
ChkUtil.tips = function (words, option1words, option1action, option2words, option2action) {
  $('#tipWords').html('<p>' + words + '</p>');
  $('#tipOption').html('<a id="regVerifyBtnTip" onclick="' + option1action + ';" class="wdialog_btn">' + option1words + '</a>');
  if (option2words && option2action) {
    $('#tipOption').append('<a onclick="' + option2action + ';" class="wdialog_btn">' + option2words + '</a>');
  }

  $('#tips').show();
  //alert('提示');
  //setTimeout("closetip()",40000);

};
//弹框隐藏
function tipshide() {
  $('#tips').hide();
};
//点击别的地方关闭弹窗
ChkUtil.closeCalendarPannel2 = function (somebutton) {
  /**
               $(document).bind("click",function(e){
                   if( !$("#tips").is(":hidden")){
                       var target  = $(e.target);
                       console.log(e.target);
                       if(target.closest("#tipscontent").length == 0 && target.closest(somebutton).length == 0){
                               $("#tips").hide();
                       }
                  }
           });
  
  */
};

//
function closetip() {
  $("#tips").hide();
}
//操作cookie
ChkUtil.getCookie = function (name) {
  var strCookies = document.cookie;
  var cookieName = name + "=";
  var valueBegin, valueEnd, value;
  valueBegin = strCookies.indexOf(cookieName);
  if (valueBegin == -1) {
    return null;
  }
  valueEnd = strCookies.indexOf(";", valueBegin);
  if (valueEnd == -1) {
    valueEnd = strCookies.length;
  }
  value = strCookies.substring(valueBegin + cookieName.length, valueEnd);
  return decodeURIComponent(value);
};
ChkUtil.saveCookie = function (name, value, expires, path, domain, secure) {
  var strCookie = name + "=" + encodeURIComponent(value);
  if (expires) {
    var curTime = new Date();
    curTime.setTime(curTime.getTime() + expires * 24 * 60 * 60 * 1000);
    strCookie += "; expires=" + curTime.toGMTString();
  }
  strCookie += (path) ? "; path=" + path : "";
  strCookie += (domain) ? "; domain=" + domain : "";
  strCookie += (secure) ? "; secure" : "";
  document.cookie = strCookie;
};
//以秒为单位的cookie
ChkUtil.saveCookie3 = function (name, value, expires, path, domain, secure) {
  var strCookie = name + "=" + encodeURIComponent(value);
  if (expires) {
    var curTime = new Date();
    //console.log(curTime.getTime());
    //console.log(curTime.getTime()+ expires * 60 *1000);
    curTime.setTime(curTime.getTime() + expires * 60 * 1000);
    strCookie += "; expires=" + curTime.toGMTString();
  }
  strCookie += (path) ? "; path=" + path : "";
  strCookie += (domain) ? "; domain=" + domain : "";
  strCookie += (secure) ? "; secure" : "";
  document.cookie = strCookie;
};
ChkUtil.checkCookieExist = function (name) {
  if (ChkUtil.getCookie(name)) {
    return true;
  } else {
    return false;
  }
};

ChkUtil.getCookie2 = function (name) {
  var strCookies = document.cookie;
  var cookieName = name + "=";
  var valueBegin, valueEnd, value;
  valueBegin = strCookies.indexOf(cookieName);
  if (valueBegin == -1) {
    return null;
  }
  valueEnd = strCookies.indexOf(";", valueBegin);
  if (valueEnd == -1) {
    valueEnd = strCookies.length;
  }
  value = strCookies.substring(valueBegin + cookieName.length, valueEnd);
  return decodeURI(value);
};
ChkUtil.saveCookie2 = function (name, value, expires, path, domain, secure) {
  var strCookie = name + "=" + encodeURI(value);
  if (expires) {
    var curTime = new Date();
    curTime.setTime(curTime.getTime() + expires * 24 * 60 * 60 * 1000);
    strCookie += "; expires=" + curTime.toGMTString();
  }
  strCookie += (path) ? "; path=" + path : "";
  strCookie += (domain) ? "; domain=" + domain : "";
  strCookie += (secure) ? "; secure" : "";
  document.cookie = strCookie;
};
ChkUtil.checkCookieExist2 = function (name) {
  if (ChkUtil.getCookie2(name)) {
    return true;
  } else {
    return false;
  }
};

/**
 *校验码简单验证 
 * @param {Object} str
 */
ChkUtil.isSmsCaptcha = function (str) {
  var patrn = /^\d{6}$/;
  return patrn.test(str);
};

//校验是否存在任何空白字符
ChkUtil.isLowerS = function (str) {
  var patrn = /\s/;
  return patrn.test(str);
};

//3.0密码规则
ChkUtil.pwdRegx = function (pwd) {
  var flag = false;
  if (pwd.length < 6 || pwd.length > 20) {
    return flag;
  } else {
    var singleFlag = ChkUtil.isW(pwd);
    var azFlag = ChkUtil.isa_z(pwd);
    var upperAZFlag = ChkUtil.isUpperA_Z(pwd);
    var digitFlag = ChkUtil.isDigit(pwd);
    if (!singleFlag && !azFlag && !upperAZFlag && !digitFlag) {
      flag = true;
    }
    return flag;
  }
};
//校验是否全是非字母字符
ChkUtil.isW = function (str) {
  var patrn = /^[\W_]+$/;
  return patrn.test(str);
};
//校验是否全是小写字母
ChkUtil.isa_z = function (str) {
  var patrn = /^[a-z]+$/;
  return patrn.test(str);
};
//校验是否全是大写字母
ChkUtil.isUpperA_Z = function (str) {
  var patrn = /^[A-Z]+$/;
  return patrn.test(str);
};
// 校验是否是整数
ChkUtil.isInteger = function (str) {
  var patrn = /^([+-]?)(\d+)$/;
  return patrn.test(str);
};
// 校验是否为正整数
ChkUtil.isPlusInteger = function (str) {
  var patrn = /^([+]?)(\d+)$/;
  return patrn.test(str);
};
// 校验是否全是数字
ChkUtil.isDigit = function (str) {
  var patrn = /^\d+$/;
  return patrn.test(str);
};
//3.0密码强度规则
ChkUtil.pwdLevel = function (value) {
  var pattern_1 = /^[\da-z]{6,7}$/;
  var pattern_2 = /^[(\W|_)A-Z]{10,20}$/;
  var pattern_3 = /\d+/;
  var pattern_4 = /[a-z]+/;
  var pattern_5 = /[A-Z]+/;
  var pattern_6 = /(\W|_)+/;
  var pattern_7 = /\S{10,20}/;
  var level = 2;
  if (pattern_1.test(value)) {
    level = 1;
  } else {
    var level31 = pattern_7.test(value) && ((pattern_3.test(value) && pattern_5.test(value) && pattern_6.test(value)) || (pattern_4.test(value) && pattern_5.test(value) && pattern_6.test(value)));
    var level32 = pattern_2.test(value);
    var level33 = pattern_3.test(value) && pattern_4.test(value) && pattern_5.test(value) && pattern_6.test(value);
    if (level31 || level32 || level33) {
      level = 3;
    }
  }
  return level;
};

// 校验电邮地址
ChkUtil.isEmail = function (str) {
  var patrn = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  return patrn.test(str);
};
//处理冒泡
ChkUtil.stopBubbleEvent = function (evt) {
  var e = evt || window.event;
  if (window.event) {
    e.cancelBubble = true;
  } else {
    e.stopPropagation();
  }
};

ChkUtil.mosaicZero = function (s) {
  return s < 10 ? '0' + s : s;
};

ChkUtil.getAgent = function () {
  var u = navigator.userAgent;
  return u;
};
// 校验是否全是数字
ChkUtil.isMoney = function (str) {
  var patrn = /^[0-9]*(\.[0-9]{1,2})?$/;
  return patrn.test(str);
};


module.exports = {
  ChkUtil: ChkUtil
}